package cn.wolfcode.education.service.impl;

import cn.wolfcode.education.domain.Employee;
import cn.wolfcode.education.domain.School;
import cn.wolfcode.education.domain.SchoolLinkman;
import cn.wolfcode.education.mapper.SchoolLinkmanMapper;
import cn.wolfcode.education.mapper.SchoolMapper;
import cn.wolfcode.education.query.QueryObject;
import cn.wolfcode.education.service.ISchoolService;
import cn.wolfcode.education.util.PageResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SchoolServiceImpl implements ISchoolService {
    @Autowired
    private SchoolMapper schoolMapper;
    @Autowired
    private SchoolLinkmanMapper schoolLinkmanMapper;

    public PageResult queryForList(QueryObject qo) {
        int count = schoolMapper.queryForCount(qo);
        if (count == 0) {
            return new PageResult();
        }
        List<School> list = schoolMapper.queryForList(qo);
        /*System.out.println(list.size());
        for (School s : list) {
            SystemDictionaryItem improtance = s.getImprotance();
            System.out.println(improtance.getName());
        }*/
        return new PageResult(count, list);
    }

    public School getSchoolById(Long id) {
        return schoolMapper.getSchoolById(id);
    }

    public void insert(School school) {
        //保存学校联系人
        Long linkmanId = school.getLinkman().getId();
        String linkmanName = school.getLinkman().getName();
        SchoolLinkman man = new SchoolLinkman();

        if (school.getLinkman() != null) {
            if (linkmanId != null) {
                //设置联系人是否是主联系人--首先设置该学校的跟进人员的main为false
                schoolLinkmanMapper.updateLinkmanMainFalseById(school.getId(), false);
                schoolLinkmanMapper.updateLinkmanMain(linkmanId, true);
            } else if (linkmanName != null) {
                //man.setSchool(school);
                man.setName(linkmanName);
                //添加到数据库
                schoolLinkmanMapper.insertLinkmanName(man);
                //设置联系人是否是主联系人--首先设置该学校的跟进人员的main为false
                schoolLinkmanMapper.updateLinkmanMainFalseById(school.getId(), false);
                schoolLinkmanMapper.updateLinkmanMain(man.getId(), true);
                school.setLinkman(man);
            }
        }
        //设置销售人员
        Employee sale = (Employee) SecurityUtils.getSubject().getPrincipal();
        school.setSale(sale);
        //设置跟单时间和跟单状态---使用默认状态
        //设置当前人员为跟进人员---

        school.setCurrentSale(sale);
        school.setFollowState(School.STATIC_LOCK);//已跟进
        //设置当前时间为 上次跟进时间
        school.setPrevTime(new Date());
        //下次跟进时间不设置
        schoolMapper.insert(school);
        //建立学校和联系人的关系
        man.setSchool(school);
        schoolLinkmanMapper.updateRelationWithSchool(man.getId(), school.getId());
    }

    public void update(School school) {
        Employee sale = (Employee) SecurityUtils.getSubject().getPrincipal();
        school.setSale(sale);
        //设置跟单时间和跟单状态---使用默认状态
        //设置当前人员为跟进人员
        //设置当前人员为跟进人员
        school.setCurrentSale(sale);
        school.setFollowState(School.STATIC_LOCK);//已跟进
        //跟进时间不更改
        //下次跟进时间不设置
        //设置联系人是否是主联系人
        Long linkmanId = school.getLinkman().getId();
        String linkmanName = school.getLinkman().getName();
        SchoolLinkman man = new SchoolLinkman();

        if (school.getLinkman() != null) {
            if (linkmanId != null) {
                //设置联系人是否是主联系人--首先设置该学校的跟进人员的main为false
                schoolLinkmanMapper.updateLinkmanMainFalseById(school.getId(), false);
                schoolLinkmanMapper.updateLinkmanMain(linkmanId, true);
            } else if (linkmanName != null) {
                //man.setSchool(school);
                man.setName(linkmanName);
                //添加到数据库
                schoolLinkmanMapper.insertLinkmanName(man);
                //设置联系人是否是主联系人--首先设置该学校的跟进人员的main为false
                schoolLinkmanMapper.updateLinkmanMainFalseById(school.getId(), false);
                schoolLinkmanMapper.updateLinkmanMain(man.getId(), true);
                school.setLinkman(man);
            }
        }
        //建立学校和联系人的关系
        schoolLinkmanMapper.updateRelationWithSchool(man.getId(), school.getId());
        school.setCurrentSale(sale);
        school.setFollowState(School.STATIC_LOCK);//已跟进
        schoolMapper.update(school);
    }

    /**
     * 丢单:只需更改跟踪状态
     *
     * @param id
     */
    public void delete(Long id) {
        schoolMapper.updateFollowStateToRemove(id, School.STATIC_REMOVE);
    }

    public void trackSchool(School school) {
        //更新大客户的当前跟单人和下次跟单时间---对象中有----直接引用
        //将原来的下次跟单时间设置为上次跟单时间
        //获取原来的下次跟单时间
        School ordSchool = schoolMapper.getSchoolById(school.getId());
        if (ordSchool.getNextTime() != null) {
            school.setPrevTime(ordSchool.getNextTime());
        } else {
            school.setPrevTime(ordSchool.getPrevTime());
        }
        //设置跟踪状态
        school.setFollowState(School.STATIC_LOCK);
        schoolMapper.updateCurrent(school);
    }

    public void signing(Long id) {
        //签约----判断签约状态
        School school = schoolMapper.getSchoolById(id);
        schoolMapper.updateState(school.getId(), !school.isState());
    }

    public void cooperation(Long id) {
        //判断合作状态
        School school = schoolMapper.getSchoolById(id);
        schoolMapper.updateHands(school.getId(), !school.isHands());
    }

    public List<School> getAllSchool() {
        return schoolMapper.selectAll();
    }
}
